package com.toymate.modules.iot.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toymate.ai.asr.AsrConfig;
import com.toymate.ai.asr.model.aliyun.AliyunAsrConfig;
import com.toymate.ai.asr.model.baidu.BaiduAsrConfig;
import com.toymate.ai.llm.LlmConfig;
import com.toymate.ai.llm.model.coze.CozeConfig;
import com.toymate.ai.llm.model.dify.DifyConfig;
import com.toymate.ai.tts.TtsConfig;
import com.toymate.ai.tts.model.aliyun.AliyunTtsConfig;
import com.toymate.ai.tts.model.azure.AzureTtsConfig;
import com.toymate.ai.tts.model.volcengine.VolcengineTtsConfig;
import com.toymate.common.constant.AiConstant;
import com.toymate.common.constant.ParamConstant;
import com.toymate.common.enums.ErrorEnum;
import com.toymate.common.exception.BusinessException;
import com.toymate.modules.iot.entity.IotRole;
import com.toymate.modules.iot.mapper.IotRoleMapper;
import com.toymate.modules.iot.service.IotRoleService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * IotRoleService
 *
 * @author weirdor (179946422@qq.com)
 * @version v1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class IotRoleServiceImpl extends ServiceImpl<IotRoleMapper, IotRole> implements IotRoleService {


    @Override
    public String getValue(String roleId, String paramCode) {
        IotRole iotRole = this.lambdaQuery().eq(IotRole::getRoleId, roleId).one();
        if (ParamConstant.LLM.equals(paramCode)) {
            JSONObject jsonObject = JSONUtil.parseObj(iotRole.getLlm());
            String llmType = jsonObject.getStr("llm_type");
            LlmConfig config = new LlmConfig();
            if (AiConstant.llmService.DIFY.getValue().equals(llmType)) {
                DifyConfig difyConfig = JSONUtil.toBean(jsonObject.getStr("llm_config"), DifyConfig.class);
                config.setLlmType(llmType);
                config.setDifyConfig(difyConfig);
            } else if (AiConstant.llmService.COUZE.getValue().equals(llmType)) {
                CozeConfig cozeConfig = JSONUtil.toBean(jsonObject.getStr("llm_config"), CozeConfig.class);
                config.setLlmType(llmType);
                config.setCozeConfig(cozeConfig);
            }
            return JSONUtil.toJsonStr(config);
        }
        if (ParamConstant.ASR.equals(paramCode)) {
            JSONObject jsonObject = JSONUtil.parseObj(iotRole.getAsr());
            String asrType = jsonObject.getStr("asr_type");
            AsrConfig config = new AsrConfig();
            if (AiConstant.sttService.BAIDU.getValue().equals(asrType)) {
                BaiduAsrConfig baiduAsrConfig = JSONUtil.toBean(jsonObject.getStr("asr_config"), BaiduAsrConfig.class);
                config.setAsrType(asrType);
                config.setBaiduAsrConfig(baiduAsrConfig);
            } else if (AiConstant.sttService.ALIYUN.getValue().equals(asrType)) {
                AliyunAsrConfig aliyunAsrConfig = JSONUtil.toBean(jsonObject.getStr("asr_config"), AliyunAsrConfig.class);
                config.setAsrType(asrType);
                config.setAliyunAsrConfig(aliyunAsrConfig);
            }
            return JSONUtil.toJsonStr(config);
        }
        if (ParamConstant.TTS.equals(paramCode)) {
            JSONObject jsonObject = JSONUtil.parseObj(iotRole.getTts());
            String ttsType = jsonObject.getStr("tts_type");
            TtsConfig config = new TtsConfig();
            if (AiConstant.ttsService.ALIYUN.getValue().equals(ttsType)) {
                AliyunTtsConfig aliyunTtsConfig = JSONUtil.toBean(jsonObject.getStr("tts_config"), AliyunTtsConfig.class);
                config.setTtsType(ttsType);
                config.setAliyunTtsConfig(aliyunTtsConfig);
            } else if (AiConstant.ttsService.VOLCENGINE.getValue().equals(ttsType)) {
                VolcengineTtsConfig volcengineTtsConfig = JSONUtil.toBean(jsonObject.getStr("tts_config"), VolcengineTtsConfig.class);
                config.setTtsType(ttsType);
                config.setVolcengineTtsConfig(volcengineTtsConfig);
            } else if (AiConstant.ttsService.AZURE.getValue().equals(ttsType)) {
                AzureTtsConfig azureTtsConfig = JSONUtil.toBean(jsonObject.getStr("tts_config"), AzureTtsConfig.class);
                config.setTtsType(ttsType);
                config.setAzureTtsConfig(azureTtsConfig);
            }
            return JSONUtil.toJsonStr(config);
        }
        return "";
    }

    @Override
    public <T> T getValueObject(String roleId, String paramCode, Class<T> clazz) {
        String paramValue = getValue(roleId, paramCode);
        if (StrUtil.isNotBlank(paramValue)) {
            return JSONUtil.toBean(paramValue, clazz);
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new BusinessException(ErrorEnum.PARAMS_TYPE_ERROR.getMessage());
        }
    }

    @Override
    public IotRole getRoleByDevice(String deviceId) {
        return baseMapper.getRoleByDevice(deviceId);
    }

}
